#!/usr/bin/python

import math
import argparse

import pygame

import generator

from game import Game
from graphics import GameView

def main():
    parser = argparse.ArgumentParser(description="PROJECT BIGGERTRON a tower"
                                     "defence game.")    
    
    parser.add_argument('--resolution',
                        dest="resolution",
                        metavar=('W', 'H'),
                        type=int,
                        nargs=2,
                        default=None,
                        help='The resolution to run the game at.')
    
    parser.add_argument('--windowed',
                        dest="windowed",
                        action="store_const",
                        const=True,
                        default=False,
                        help="Run in windowed mode.")
    
    args = parser.parse_args()
    
    ###
    
    pygame.display.init()
    
    modes = pygame.display.list_modes()
    
    WIDTH, HEIGHT = modes[0] if args.resolution is None else args.resolution
    
    pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN if not args.windowed else False)
    pygame.display.set_caption("PROJECT BIGGERTRON") 
    screen = pygame.display.get_surface()     
    
    GRIDSIZE = 8
    
    hc = (1 / math.cos(math.radians(30)))
    
    w, h = 60, 45
    
    def mapgen():
        return generator.generate_map(WIDTH // GRIDSIZE, int((HEIGHT * hc) // GRIDSIZE), GRIDSIZE)
    
    # this is to test committing to default rather than development
    
    gamemap = mapgen()
    
    game = Game(mapgen)
    view = GameView(game, (WIDTH, HEIGHT))
    
    quit = False
    
    while not quit:
        pygame.time.wait(1)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit = True
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                quit = True
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                game = Game(mapgen)
                view = GameView(game, (WIDTH, HEIGHT))
        
        screen.fill((0, 0, 0))
        
        game.update()
        view.draw(screen)
        
        pygame.display.flip()

if __name__ == '__main__':
    main()
